package com.jinlejia.erp.ui.home;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.jinlejia.erp.R;
import com.jinlejia.erp.databinding.ActivityBuyQualificationBinding;

/**
 * 购房资格
 */
public class BuyQualificationActivity extends AppCompatActivity {

    private ActivityBuyQualificationBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_buy_qualification);
        binding.setEvent(this);
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.married_expand_btn:
                changeState(1);
                break;
            case R.id.singlehood_expand_btn:
                changeState(2);
                break;
            case R.id.divorced_expand_btn:
                changeState(3);
                break;
            case R.id.continuous_social_security_expand_btn:
                changeState(4);
                break;
            case R.id.break_off_social_security_expand_btn:
                changeState(5);
                break;
            default:
                break;
        }
    }

    public void changeState(int i) {
        Drawable drawableDown = getResources().getDrawable(R.mipmap.ic_drop_down);
        drawableDown.setBounds(0, 0, drawableDown.getMinimumWidth(), drawableDown.getMinimumHeight());
        Drawable drawableup = getResources().getDrawable(R.mipmap.arrow_up);
        drawableup.setBounds(0, 0, drawableup.getMinimumWidth(), drawableup.getMinimumHeight());
        switch (i) {
            case 1:
                //如果要展开当前项，先关闭其他
                if (!binding.marriedExpandable.isExpanded()) {
                    collapse();
                }
                binding.marriedExpandable.toggle();
                binding.marriedExpandBtn.setText(binding.marriedExpandable.isExpanded() ? "收起详情" : "展开详情");
                binding.marriedExpandBtn.setCompoundDrawables(null,null,
                        binding.marriedExpandable.isExpanded() ?drawableup: drawableDown,null);
                binding.scrollView.postDelayed(() -> binding.scrollView.smoothScrollTo(0, 0),300);

                break;
            case 2:
                //如果要展开当前项，先关闭其他
                if (!binding.singlehoodExpandable.isExpanded()) {
                    collapse();
                }
                binding.singlehoodExpandable.toggle();
                binding.singlehoodExpandBtn.setText(binding.singlehoodExpandable.isExpanded() ? "收起详情" : "展开详情");
                binding.singlehoodExpandBtn.setCompoundDrawables(null,null,
                        binding.singlehoodExpandable.isExpanded() ?drawableup:
                                drawableDown,null);
                binding.scrollView.postDelayed(() -> binding.scrollView.smoothScrollTo(0, binding.singlehoodLayout.getTop()), 300);

                break;
            case 3:
                //如果要展开当前项，先关闭其他
                if (!binding.divorcedExpandable.isExpanded()) {
                    collapse();
                }
                binding.divorcedExpandable.toggle();
                binding.divorcedExpandBtn.setText(binding.divorcedExpandable.isExpanded() ? "收起详情" : "展开详情");
                binding.divorcedExpandBtn.setCompoundDrawables(null,null,
                        binding.divorcedExpandable.isExpanded() ?drawableup:
                                drawableDown,null);
                binding.scrollView.postDelayed(() -> binding.scrollView.smoothScrollTo(0, binding.divorcedLayout.getTop()), 300);
                break;
            case 4:
                //如果要展开当前项，先关闭其他
                if (!binding.continuousSocialSecurityExpandable.isExpanded()) {
                    collapse();
                }
                binding.continuousSocialSecurityExpandable.toggle();
                binding.continuousSocialSecurityExpandBtn.setText(binding.continuousSocialSecurityExpandable.isExpanded() ? "收起详情" : "展开详情");
                binding.continuousSocialSecurityExpandBtn.setCompoundDrawables(null,null,
                        binding.continuousSocialSecurityExpandable.isExpanded() ?drawableup:
                                drawableDown,null);
                binding.scrollView.postDelayed(() -> binding.scrollView.smoothScrollTo(0, binding.continuousLayout.getTop()), 300);
                break;
            case 5:
                //如果要展开当前项，先关闭其他
                if (!binding.breakOffSocialSecurityExpandable.isExpanded()) {
                    collapse();
                }
                binding.breakOffSocialSecurityExpandable.toggle();
                binding.breakOffSocialSecurityExpandBtn.setText(binding.breakOffSocialSecurityExpandable.isExpanded() ? "收起详情" : "展开详情");
                binding.breakOffSocialSecurityExpandBtn.setCompoundDrawables(null,null,
                        binding.breakOffSocialSecurityExpandable.isExpanded() ?drawableup:
                                drawableDown,null);
                binding.scrollView.postDelayed(() -> binding.scrollView.smoothScrollTo(0, binding.breakOffLayout.getTop()), 300);
                break;
            default:
                break;
        }

    }

    private void collapse() {

        Drawable rightDrawable = getResources().getDrawable(R.mipmap.ic_drop_down);
        rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());

        binding.marriedExpandable.setExpanded(false);
        binding.marriedExpandBtn.setText("展开详情");
        binding.marriedExpandBtn.setCompoundDrawables(null,null,rightDrawable,null);

        binding.singlehoodExpandable.setExpanded(false);
        binding.singlehoodExpandBtn.setText("展开详情");
        binding.singlehoodExpandBtn.setCompoundDrawables(null,null,rightDrawable,null);

        binding.divorcedExpandable.setExpanded(false);
        binding.divorcedExpandBtn.setText("展开详情");
        binding.divorcedExpandBtn.setCompoundDrawables(null,null,rightDrawable,null);

        binding.continuousSocialSecurityExpandable.setExpanded(false);
        binding.continuousSocialSecurityExpandBtn.setText("展开详情");
        binding.continuousSocialSecurityExpandBtn.setCompoundDrawables(null,null,rightDrawable,null);

        binding.breakOffSocialSecurityExpandable.setExpanded(false);
        binding.breakOffSocialSecurityExpandBtn.setText("展开详情");
        binding.breakOffSocialSecurityExpandBtn.setCompoundDrawables(null,null,rightDrawable,null);

    }

}
